﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MpSoft.Library.DTO;
using System.Text;
using MpSoft.Library.BUSS.Categories;
using MpSoft.Library.BUSS.Products;
using MpSoft.Library.CommonLibrary;
using MpSoft.Basic.Utils;
namespace MpSoft.Web.Controls.Product
{
    public partial class ProductDetailControl : System.Web.UI.UserControl
    {

        #region Var

        private int ItemID = 0;
        private Library.DTO.Product product = null;

        #endregion
        
        #region Method

        private void BindControl()
        {
            if (ItemID > 0)
            {
                product = ProductBUSS.Product_ByID(ItemID);
                var listOrther = ProductBUSS.Product_List(Product.CategoryID, 1, 1, false, false, false, false, 6);
                rpList.DataSource = listOrther;
                rpList.DataBind();
            }
        }

        protected string ShowPrice(double price, double pricenew)
        {
            if (price > 0 && pricenew > 0)
                return "<p>Giá gốc: <b class='price-old'>" + Filter.Money(price.ToString()) + " VNĐ</b></p>" +
                    "<p>Giá KM: <b class='price-new'>" + Filter.Money(pricenew.ToString()) + " VNĐ</b></p>";
            if (price > 0)
                return "<p>Giá gốc: <b class='price'>" + Filter.Money(price.ToString()) + " VNĐ</b></p>";
            return "<p>Giá SP: <b class='price'> Liên hệ</b></p>";
        }

        protected string GetCategoy(string categoryid)
        {
            if (!string.IsNullOrEmpty(categoryid))
            {
                var list = GetParent(int.Parse(categoryid));
                if (list.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = list.Count - 1; i > -1; i--)
                        sb.Append("<a href='" + SEOHelper.GetCategoryUrl(list[i].SeName) + "'>" + list[i].CategoryName + "</a>");
                    return sb.ToString();
                }
            }
            return "";
        }

        private List<Category> GetParent(int parentid)
        {
            var obj = CategoryBUSS.Category_GetByID(parentid);
            List<Category> list = new List<Category>();
            while (obj != null)
            {
                list.Add(obj);
                obj = CategoryBUSS.Category_GetByID(obj.ParentID);
            }
            return list;
        }

        #endregion

        #region Event
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ItemID"]))
                int.TryParse(Request.QueryString["ItemID"].ToString(), out ItemID);
            BindControl();
        }
        
        #endregion

        #region Properties

        public Library.DTO.Product Product { get { return product; } }

        #endregion
    }
}